package com.nsgf.library.ui.adapter;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.nsgf.library.data.bean.Shop;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ItemCartBinding;
import com.zhht.htzx.databinding.ItemConfirmGoodsBinding;

import java.util.List;

public class ConfirmOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_SHOP = 0;
    private static final int TYPE_ITEM = 1;

    private List<Shop.ValidListBean> shops;
    private Context context;
    private boolean isUpdating = false;

    public ConfirmOrderAdapter(Context context, List<Shop.ValidListBean> shops) {
        this.context = context;
        this.shops = shops;
    }

    @Override
    public int getItemViewType(int position) {
        int count = 0;
        for (Shop.ValidListBean shop : shops) {
            if (position == count) {
                return TYPE_SHOP;
            }
            count += 1 + shop.getSpecifications().size();
        }
        return TYPE_ITEM;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_SHOP) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_confirm_order_header, parent, false);
            return new ShopViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_confirm_goods, parent, false);
            return new CartViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ShopViewHolder) {
            ((ShopViewHolder) holder).bind(getShopByPosition(position));
        } else if (holder instanceof CartViewHolder) {
            Shop.ValidListBean.SpecificationsBean item = getItemByPosition(position);
            ((CartViewHolder) holder).bind(item, getShopPositionByItemPosition(position));
        }
    }

    @Override
    public int getItemCount() {
        int count = 0;
        for (Shop.ValidListBean shop : shops) {
            count += 1 + shop.getSpecifications().size();
        }
        return count;
    }

    private Shop.ValidListBean getShopByPosition(int position) {
        int count = 0;
        for (Shop.ValidListBean shop : shops) {
            if (position == count) {
                return shop;
            }
            count += 1 + shop.getSpecifications().size();
        }
        return null;
    }

    private Shop.ValidListBean.SpecificationsBean getItemByPosition(int position) {
        int count = 0;
        for (Shop.ValidListBean shop : shops) {
            count++;
            if (position < count + shop.getSpecifications().size()) {
                return shop.getSpecifications().get(position - count);
            }
            count += shop.getSpecifications().size();
        }
        return null;
    }

    private int getShopPositionByItemPosition(int position) {
        int count = 0;
        for (int i = 0; i < shops.size(); i++) {
            if (position < count + 1 + shops.get(i).getSpecifications().size()) {
                return i;
            }
            count += 1 + shops.get(i).getSpecifications().size();
        }
        return -1;
    }

    class ShopViewHolder extends RecyclerView.ViewHolder {
        private TextView shopName;

        public ShopViewHolder(@NonNull View itemView) {
            super(itemView);
            shopName = itemView.findViewById(R.id.shop_name);
        }

        public void bind(Shop.ValidListBean shop) {
            if (shop == null) return;
            shopName.setText(shop.getTenantName());
        }
    }

    class CartViewHolder extends RecyclerView.ViewHolder {
        private ItemConfirmGoodsBinding itemCartBinding;

        public CartViewHolder(@NonNull View itemView) {
            super(itemView);
            itemCartBinding = DataBindingUtil.bind(itemView);
        }

        public void bind(Shop.ValidListBean.SpecificationsBean item, int shopPosition) {
            if (item == null) return;
            itemCartBinding.setVm(item);
            // 移除之前的监听器，避免重复触发
            if (!item.isSelected()) {
                itemCartBinding.getRoot().setVisibility(GONE);
            } else {
                itemCartBinding.getRoot().setVisibility(VISIBLE);
            }
        }
    }
}